//
//  MYPublishEditCollCell.swift
//  MuYing
//
//  Created by tbb on 2019/8/15.
//  Copyright © 2019 TBB. All rights reserved.
//

import UIKit

class MYPublishEditCollCell: YQTableViewBaseCell,UICollectionViewDelegate,UICollectionViewDataSource {

    @objc var imageArr = [UIImage]() {
        didSet {
            self.imageCollView.reloadData();
        }
    }
    
    @objc var imageUrlArr = [String]() {
        didSet {
            self.imageCollView.reloadData();
        }
    }
    
    
    lazy var imageCollView: UICollectionView = {
        let layout = UICollectionViewFlowLayout();
        layout.minimumLineSpacing = 7.0;
        layout.minimumInteritemSpacing = 7.0;
        var w = (kSwiftkScreenSizeWidth - 35) / 4.0;
        layout.itemSize = CGSize(width: w, height: w);
        let imageCollView = UICollectionView(frame: CGRect(x: 7, y: 14, width: kSwiftkScreenSizeWidth-14, height: self.height - 28), collectionViewLayout: layout)
        imageCollView.backgroundColor = UIColor.white;
        imageCollView.delegate = self;
        imageCollView.dataSource = self;
        imageCollView.register(UINib(nibName: "MYPublishEditCollViewCell", bundle: nil), forCellWithReuseIdentifier: "MYPublishEditCollViewCell");
        return imageCollView;
    }()
    
    override func layoutSubviews() {
        super.layoutSubviews();
        self.contentView.addSubview(self.imageCollView);
        self.imageCollView.mas_makeConstraints { (make) in
            make?.top.mas_equalTo()(14);
            make?.bottom.mas_equalTo()(-14);
            make?.left.mas_equalTo()(7);
            make?.right.mas_equalTo()(-7);
        }
    }
    
}

extension MYPublishEditCollCell {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.imageArr.count;
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MYPublishEditCollViewCell", for: indexPath) as? MYPublishEditCollViewCell else { return UICollectionViewCell() };
        cell.imageView.image = self.imageArr[indexPath.item];
        return cell;
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
//        let cell = [collectionView .cellForItem(at: indexPath)];
//        let imageBrowser = YQBaseImageBrowserVC.init(imageArrUrl: self.imageUrlArr , indexPath.item,: UInt(index), sourceObject: cell);
//        imageBrowser.currentPage = index;
//        imageBrowser.show();
        let cell = [collectionView .cellForItem(at: indexPath)];
        let imageBrowser = YQBaseImageBrowserVC.init(imageArrUrl: self.imageUrlArr, currentIndex: UInt(indexPath.item), sourceObject: cell);
        imageBrowser.currentPage = indexPath.item;
        imageBrowser.show();
        
        
    }
}
